/*
 * Copyright (C) 2008 GeoScheduler Team, as stated on <http://www.geoscheduler.org/authors>.
 * 
 * This file is part of GeoScheduler.
 * 
 * GeoScheduler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GeoScheduler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with GeoScheduler. If not, see <http://www.gnu.org/licenses/>.
 */
package org.geoscheduler.commons.tools;

import java.text.MessageFormat;

/**
 * <h2>AssertTools</h2>
 * <p>
 * Assert utility class
 * </p>
 * 
 * @author lko
 */
public class AssertTools {

	/**
	 * Assert that <code>value</code> and <code>expected</code> are equal.
	 * 
	 * @return <code>value</code> if equals <code>expected</code>
	 * @throws IllegalArgumentException
	 *             if <code>value</code> is not equal with
	 *             <code>expected</code>
	 */
	public static <T> T assertEquals(T value, T expected) {
		return assertEquals(value, expected, "Expected value %s but %s given", String.valueOf(expected), String
				.valueOf(value));
	}

	/**
	 * Assert that <code>value</code> and <code>expected</code> are equal.
	 * 
	 * @param value
	 * @param expected
	 * @param pattern -
	 *            message pattern used for exception message
	 * @param arguments -
	 *            arguments for message pattern
	 * @return <code>value</code> if equals <code>expected</code>
	 * @throws IllegalArgumentException
	 *             if <code>value</code> is not equal with
	 *             <code>expected</code>
	 */
	public static <T> T assertEquals(T value, T expected, String pattern, Object... arguments) {
		if (!expected.equals(value)) {
			throw new IllegalArgumentException(MessageFormat.format(pattern, arguments));
		}
		return value;
	}

	/**
	 * Assert that value is not <code>null</code>.
	 * 
	 * @param value
	 * @return <code>value</code> if value is not <code>null</code>
	 * @throws IllegalArgumentException
	 *             if <code>value</code> is <code>null</code>
	 * 
	 */
	public static <T> T assertNotNull(T value) {
		return assertNotNull(value, "Not null value expected");
	}

	/**
	 * Assert that value is not <code>null</code>.
	 * 
	 * @param pattern -
	 *            exception message pattern
	 * @param arguments -
	 *            arguments for exception message
	 * @param value
	 * @return <code>value</code> if value is not <code>null</code>
	 * @throws IllegalArgumentException
	 *             if <code>value</code> is <code>null</code>
	 */
	public static <T> T assertNotNull(T value, String pattern, Object... arguments) {
		if (value == null) {
			throw new IllegalArgumentException(MessageFormat.format(pattern, arguments));
		}
		return value;
	}

	/**
	 * Assert that value has specified type
	 * 
	 * @param value
	 * @param type
	 * @return <code>value</code> if is assignable to type.
	 * @throws IllegalArgumentException
	 *             if <code>value</code> not assignable to type
	 */
	public static <T> T assertType(Object value, Class<T> type) {
		return assertType(value, type, "Type %s expected", type.getClass().getName());
	}

	/**
	 * Assert that value has specified type
	 * 
	 * @param value
	 * @param type
	 * @param pattern
	 *            exception message pattern
	 * @param arguments
	 *            arguments for exception message pattern
	 * @return <code>value</code> if is assignable to type.
	 * @throws IllegalArgumentException
	 *             if <code>value</code> is not assignable to type
	 */
	public static <T> T assertType(Object value, Class<T> type, String pattern, Object... arguments) {
		if (value != null && !type.isAssignableFrom(value.getClass())) {
			throw new IllegalArgumentException(MessageFormat.format(pattern, arguments));
		}
		return type.cast(value);
	}

}
